<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id 
 * @property string $imei_md5 
 * @property string $os 
 * @property string $ip
 * @property string $ts 
 * @property int $pid 
 * @property int $uid 
 * @property int $aid 
 * @property string $oaid 
 * @property string $oaid_md5 
 * @property string $click_id 
 * @property string $callback_url 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 */
class OcpcBaiduSearch extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_baidu_search';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'pid' => 'integer', 'uid' => 'integer', 'aid' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];


    /**
     * 根据oaid查询是否存在
     * @param string $oaid
     * @return bool
     */
    public static function existsOaid(string $oaid): bool
    {
        if (empty($oaid)) {
            return false;
        }

        return self::where('oaid', $oaid)
//            ->where('channel_id', $channel)
            ->exists();
    }

    /**
     * 根据imei查询是否存在
     * @param string $muid
     * @return bool
     */
    public static function existImei(string $muid): bool
    {
        if (empty($imei)) {
            return false;
        }

        return self::where([
            'imei_md5'    => $muid,
//            'channel_id' => $channel
        ])->exists();
    }

    /**
     * @param string $oaid_md5
     * @return bool
     */
    public static function existOaidMd5(string $oaid_md5): bool
    {
        if (empty($oaid_md5)) {
            return false;
        }

        return self::where([
            'oaid_md5' => $oaid_md5,
        ])->exists();
    }

    /**
     * @param array $params
     * @return int
     */
    public static function addLog(array $params = []): int
    {
        $data = array_merge($params,[
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ]);

        $data = [
            'imei_md5'      => $params['imei_md5'] ?? '',
            'os'        => $params['os'] ?? '',
            'ip'        => $params['ip'] ?? '',
//            'ua'   => $params['ua'] ?? '',
            'ts'     => $params['ts'] ?? '',
            'pid'         => $params['pid'] ?? '',
            'uid'          => $params['uid'] ?? '',
            'aid'        => $params['aid'],
            'oaid'         => $params['oaid'] ?? '',
            'oaid_md5'          => $params['oaid_md5'] ?? '',
            'click_id'        => $params['click_id'],
            'callback_url'         => $params['callback_url'] ?? '',
            'created_at'  => date('Y-m-d H:i:s'),
            'updated_at'  => date('Y-m-d H:i:s')
        ];

        return self::insertGetId($data);
    }


    /**
     * 处理查询
     * @param $info
     * @return array
     */
    public static function handleSearch($info): array
    {
        log_info(__CLASS__.':'.json_encode($info));
        echo 6;
        //oaid 查询
        if (isset($info['oaid']) && $info['oaid']) {
            $resF = self::where('oaid', $info['oaid'])
                ->orderBy('created_at', "desc")->first(['id', 'imei_md5','oaid_md5','oaid','callback_url']);
            if ($resF) {
                return $resF->toArray();
            }


            $res = self::where('oaid_md5', strtolower(md5($info['oaid'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'imei_md5','oaid_md5','oaid','callback_url']);

            if ($res) {
                return $res->toArray();
            }
        }

        if (isset($info['imei']) && $info['imei']) {
            $res = self::where('imei_md5', strtolower(md5($info['imei'])))
//                ->where("channel_id", $info['channel'])
                ->orderBy('created_at', "desc")->first(['id', 'imei_md5','oaid_md5','oaid','callback_url']);

            if ($res) {
                return $res->toArray();
            }
        }

        return [];
    }
}